package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.adapter2.Shengqianxrecycleviewadpter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.LivingmoneyLog;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc
 * @auth Mr Wu Fan
 * Created by you on 2018/3/29.
 */

public class Fragmentsheng  extends Fragment{
    private  int startindex=0;
    private  int lengthindex=10;
    private XRecyclerView xRecyclerView;
    private Shengqianxrecycleviewadpter shengqianxrecycleviewadpter;
    final List<LivingmoneyLog.DataBean> list = new ArrayList<>();
    private ImageView img;
    private TextView tv;
    private  int lodingint  ;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.item,null);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.shengqian_xrecycle);
        img = (ImageView) view.findViewById(R.id.imgwushuju);
        tv = (TextView) view.findViewById(R.id.jilu);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingMoreEnabled(true);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        xRecyclerView.setLayoutManager(manager);

        if (shengqianxrecycleviewadpter == null){
            shengqianxrecycleviewadpter =  new Shengqianxrecycleviewadpter(list, getActivity());
            xRecyclerView.setAdapter(shengqianxrecycleviewadpter);
        }else {
            shengqianxrecycleviewadpter.notifyDataSetChanged();
        }
        getnetMingxi(startindex, lengthindex);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        lodingint=0;
                        startindex=0;
                        lengthindex=10;
                        getnetMingxi(startindex, lengthindex);
                        shengqianxrecycleviewadpter.setData(list);
                        xRecyclerView.refreshComplete();

                    }
                },1000);
            }

            @Override
            public void onLoadMore() {
                lodingint=0;
                startindex++;
                lengthindex++;
                getnetMingxi(startindex, lengthindex);
                xRecyclerView.loadMoreComplete();

            }
        });

        return view;
    }

    private List<LivingmoneyLog.DataBean> getnetMingxi(int startindex , int lengthindex) {

        String url = ApiManager.LiMonLiLOG+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity())+"&start="+startindex+"&length="+lengthindex;
       // Log.e("生钱记录","主题url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("shenglog",responseInfo.result);
                LivingmoneyLog livingmoneyLog = new Gson().fromJson(responseInfo.result, LivingmoneyLog.class);
                String code = livingmoneyLog.getCode();
                if (code.equals("10000")){
                    List<LivingmoneyLog.DataBean> data = livingmoneyLog.getData();
                    list.addAll(data);

                    // adapter(data);

                    shengqianxrecycleviewadpter.setData(data);

                    //判断是否有数据
                    if (data.size()==0){
                        img.setVisibility(View.VISIBLE);
                        tv.setVisibility(View.VISIBLE);
                    }else {
                        img.setVisibility(View.GONE);
                        tv.setVisibility(View.GONE);
                    }


                }else {
                    Toast.makeText(getActivity(), livingmoneyLog.getMsg(), Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();

            }
        });
        return list;


    }

  /*  private void adapter(List<LivingmoneyLog.DataBean> data) {
        shengqianxrecycleviewadpter = new Shengqianxrecycleviewadpter(data, getActivity());
        xRecyclerView.setAdapter(shengqianxrecycleviewadpter);
    }*/


}
